﻿using System;
using System.IO;
using System.Reflection;
using System.Threading;
using System.Windows.Forms;

namespace VolgaTransTelecomClient
{
    internal static class Program
    {
        #region Fields
        private static TrafficChecker _trafficChecker;
        #endregion

        #region Methods
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        private static void Main()
        {
            Application.ThreadException += new ThreadExceptionEventHandler(Application_ThreadException);
            Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException);
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);
            _trafficChecker = new TrafficChecker();
            _trafficChecker.Initialize();
            Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
            Application.Run();
        }

        /// <summary>
        /// Handles the ThreadException event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Threading.ThreadExceptionEventArgs"/> instance containing the event data.</param>
        private static void Application_ThreadException(object sender, ThreadExceptionEventArgs e)
        {
            Logger.WriteError(e.Exception);
        }

        /// <summary>
        /// Handles the UnhandledException event of the CurrentDomain control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.UnhandledExceptionEventArgs"/> instance containing the event data.</param>
        private static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            Logger.WriteError(e.ExceptionObject.ToString());
        }

        /// <summary>
        /// Handles the ApplicationExit event of the Application control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
        private static void Application_ApplicationExit(object sender, EventArgs e)
        {
            if (_trafficChecker != null)
            {
                _trafficChecker.Dispose();
                _trafficChecker = null;
            }
            Application.ApplicationExit -= new EventHandler(Application_ApplicationExit);
            Application.ThreadException -= new ThreadExceptionEventHandler(Application_ThreadException);
        }
        #endregion
    }
}
